# configure.in for CIL           -*- sh -*-
# Process this file with autoconf to produce a configure script.

# Autoconf runs this through the M4 macroprocessor first; lines
# starting with "dnl" are comments to M4.  The result is a bash
# script; any text which isn't an M4/autoconf directive gets
# copied verbatim to that script.

# also, in general, watch out: the M4 quoting charactes are
# the square brackets: [ and ].  if you want to pass brackets
# to something, you can quote the brackets with more brackets.
# I don't know how to pass a single (unbalanced) bracket ..

# sm: changed this file to use '#' for comments, since that's
# just as good (since this becomes an 'sh' script)




# -------------- usual initial stuff -------------
# this simply names a file somewhere in the source tree to verify
# we're in the right directory
AC_INIT(src/cil.mli)
AC_CONFIG_HEADERS(config.h)

# sm: require a late-enough autoconf; this is the version number
# that's on manju, so I assume it's ok
AC_PREREQ(2.50)

#
# Assign here the CIL version numbers
CIL_VERSION_MAJOR=1
CIL_VERSION_MINOR=4
CIL_VERSION_REV=0
CIL_VERSION=$CIL_VERSION_MAJOR.$CIL_VERSION_MINOR.$CIL_VERSION_REV


# make sure I haven't forgotten to run autoconf
if test configure -ot configure.in; then
  AC_MSG_ERROR(configure is older than configure.in; you forgot to run autoconf)
fi

# check for C compiler; this typically finds gcc; it sets the
# variable CC to whatever it finds, which then gets substituted
# for @CC@ in output files; you have to do this even if you don't
# care about @CC@, because system feature tests later on in
# the ./configure script will expect $CC to be set right
AC_PROG_CC

AC_PROG_INSTALL

# find system type (using this macro means we must include
# the files install-sh, config.sub, and config.guess (all from
# the autoconf distribution) in our source tree!)
AC_CANONICAL_SYSTEM


# ---------------- generic functions -----------------
# debugging diagnostic; set to 'echo' to debug or 'true' for production
# (technically you're not supposed to use shell functions in
# configure scripts, because some-obscure-sh somewhere doesn't
# support them.. but they're too convenient to not use)
diagnostic() {
  #echo "$@"
  true "$@"
}

# determine if a binary is in the path
binaryExists() {
  # on cygwin, 'which' always returns success, so use 'type' instead
  if type "$1" >/dev/null 2>&1; then
    return 0
  else
    return 1
  fi
}


# -------------- portable configuration ----------------
# this specifies the root of the source tree; it's just the
# directory where ./configure runs, except on cygwin, which
# overrides this below
CILHOME=`pwd`

DEFAULT_COMPILER=_GNUCC
DEFAULT_CIL_MODE=GNUCC

# is the microsoft compiler available?
# hmm.. I think we should check the version or something, because
# sometimes people have Common Lisp's interpreter called 'cl' ..
AC_MSG_CHECKING(for msvc cl.exe (optional))
# See if CC points to the MS compiler
if "$CC" 2>&1 | grep "Microsoft" >/dev/null; then 
  AC_MSG_RESULT([found, set as default])
  HAS_MSVC=yes
  DEFAULT_COMPILER=_MSVC
  DEFAULT_CIL_MODE=MSVC
else
  if cl 2>&1 | grep "Microsoft" >/dev/null ;then
     AC_MSG_RESULT(found)
     HAS_MSVC=yes
  else
     AC_MSG_RESULT(not found)
     HAS_MSVC=no
  fi
fi

# is ocaml available?
# needed binaries: ocamllex ocamlyacc ocamldep ocamlopt ocamlc
ocamlDownloadInstructions="
      OCaml can be downloaded from http://caml.inria.fr/ocaml/.
      After downloading and unpacking the source distribution, in the ocaml
      directory, do
        ./configure
        make world
        make opt
        make install
      Then come back here and re-run ./configure."

# required major/minor. 
# required major/minor
reqMaj=3
reqMin=08
knownMaj=3
knownMin=12
AC_MSG_CHECKING(ocaml version is at least $reqMaj.$reqMin)
if binaryExists ocamlc; then
  # what version?
  ver=`ocamlc -v | grep version | sed 's/^.*version //'`
  diagnostic "ver is $ver"
  # major: anything before the .
  major=`echo $ver | sed 's/\..*$//'`
  diagnostic "major is $major"
  # minor: numbers after the .
  # (the outer level of bracket-quotation protects the inner brackets)
  [minor=`echo $ver | sed 's/^[^.]*\.\([0-9][0-9]*\).*$/\1/'`]
  diagnostic "minor is $minor"

  # I would think autoconf would already have a facility for doing
  # these kinds of major/minor version checks, but I can't find it
  if test $major -gt $reqMaj -o $major -ge $reqMaj -a $minor -ge $reqMin; then
    AC_MSG_RESULT([version is $ver, ok])

    # sm: added this test when we found that CCured needed to be changed
    # a little when 3.06 came out (it had previously worked with 3.04)
    if test "$major" -gt $knownMaj -o "$major" -ge $knownMaj -a "$minor" -gt $knownMin; then
      AC_MSG_WARN([Your ocaml version is $ver, but the latest version this program
                   is known to work with is $knownMaj.$knownMin.  If you have
                   trouble compiling, please try using an earlier version
                   or see if there is a later version of this program.])
    fi
  else
    AC_MSG_ERROR([
      I found OCaml version $ver; this program requires at least $reqMaj.$reqMin.
      Please download a newer OCaml distribution.
      $ocamlDownloadInstructions
    ])
  fi

  # check for existence of other binaries
  AC_MSG_CHECKING(existence of related ocaml tools)
  if binaryExists ocamllex && \
     binaryExists ocamlyacc && \
     binaryExists ocamldep && \
     binaryExists ocamlopt; then
    AC_MSG_RESULT(ok)
  else
    AC_MSG_ERROR([
      At least one of ocamllex, ocamlyacc, ocamldep or ocamlopt is missing.
      In particular, ocamlopt requires you to "make opt" when building
      OCaml from source.  Please make sure all these tools are built and
      in the path.
    ])
  fi
else
  AC_MSG_ERROR([
      The "ocamlc" OCaml compiler was not found in the path: $PATH.

      Most of this program is written in the OCaml language, so its compiler
      is required.
      $ocamlDownloadInstructions
  ])
fi

#
# ------------------- Perl ----------------
#
AC_MSG_CHECKING([for Perl])
  if ! binaryExists perl; then
    AC_MSG_ERROR([
      perl not found.
      You need perl version 5.6.1 or later for CIL.
      You can get perl at http://www.cpan.org/src/index.html .
    ])
  fi

  # sm: oh how nice it would be to just say "use English;
  # print($PERL_VERSION)", but that appears broken on 5.6.1.. so I'm
  # trying to say "caret right-bracket", but then that would run afoul
  # of autoconf's quoting characters, so I use the "quadrigraph" @:>@
  # to stand for right-bracket.  what a mess.
  perlver=`perl -e 'print($@:>@);'`
  if perl -e "exit( $perlver >= 5.006001 );"; then
    AC_MSG_ERROR([
      Found perl version $perlver, but at least 5.6.1 is required.
      You can get a newer perl at http://www.cpan.org/src/index.html .
    ])
  fi

  perlport=`perl -e "print $^O;"`
  case "$perlport" in
    cygwin)
        ;;
    msys)
        ;;
    MSWin32) # ActivePerl
        ;;
    linux)
        ;;
    freebsd)
        ;;
    openbsd)
        ;;
    darwin) # Mac OS X
        ;;
    solaris)
	;;
    *)
      AC_MSG_ERROR([
        Unsupported Perl port $perlport -- sorry.
        cygwin, MSWin32 (ActivePerl), linux, freebsd, openbsd, darwin,
        and solaris are the supported ports.
      ])
  esac
AC_MSG_RESULT([found version $perlver, port $perlport])

        # The cygwin port has some bugs in the File::Spec module
if test "$perlport" = "cygwin" ;then
  AC_MSG_CHECKING([for known cygwin Perl bug in File::Spec])
  perlfixres=[`perl -e '
      use File::Spec;
      if(File::Spec->file_name_is_absolute("C:/test")) {
        print "no bug found"; exit 0;
      } else {
        print "bug";
        foreach $d (@INC) {
           if(-f "$d/File/Spec/Unix.pm") {
              open(IN, "<$d/File/Spec/Unix.pm");
              open(OUT, ">$d/File/Spec/Unix.pm.fixed") 
                       || die "Cannot open $d/File/Spec/Unix.pm.fixed";
              while(<IN>) {
                if($_ =~ m|sub file_name_is_absolute|) {
                   print OUT $_;
                   print OUT scalar(<IN>);
                   print OUT <<EOF;
   if(\\$^O eq \"cygwin\") { 
       return scalar(\\$file =~ m{^(\\[a-z\\]:)?\\[\\\\\\\\/\\]}is);
};
EOF
                   next;
                }
                print OUT $_;
              }
              close(OUT);
              close(IN);
              system("mv -f $d/File/Spec/Unix.pm.fixed $d/File/Spec/Unix.pm");
           }
        }
      }
  '`]
  # See if it was indeed fixed
  if test "$perlfixres" = "bug" ;then
     perlfixres=`perl -e '
      use File::Spec;
      if(File::Spec->file_name_is_absolute("C:/test")) {
        print "bug fixed"; exit 0;
      } else {
        print "cannot fix bug"; exit 1;
      }'`
  fi
  if test "x$perlfixres" = "x" ;then
      AC_MSG_ERROR([
        Cannot run perl
      ])
  elif test "$perlfixres" = "cannot fix bug" ;then
      AC_MSG_ERROR([
        Found a bug but cannot fix it.
      ])
  else
     AC_MSG_RESULT([$perlfixres])
 fi
fi

# additional tools we might check for:
#   - gnu make

#
# -------------------- GCC --------------
#

AC_MSG_CHECKING([for gcc version])
AC_CHECK_TYPE(__builtin_va_list,
              HAVE_BUILTIN_VA_LIST=true,
              HAVE_BUILTIN_VA_LIST=false)
AC_MSG_CHECKING([if __thread is a keyword])
AC_COMPILE_IFELSE([int main(int __thread) { return 0; }],
                  THREAD_IS_KEYWORD=false,
                  THREAD_IS_KEYWORD=true)
AC_MSG_RESULT($THREAD_IS_KEYWORD)

# Does gcc add underscores to identifiers to make assembly labels?
# (I think MSVC always does)
AC_MSG_CHECKING([if gcc adds underscores to assembly labels.])
AC_LINK_IFELSE([int main() { __asm__("jmp _main"); }],
                  UNDERSCORE_NAME=true,
                  UNDERSCORE_NAME=false)
AC_MSG_RESULT($UNDERSCORE_NAME)


# ----------- some stuff 'autoscan' put here --------------
# (autoscan is part of the autoconf distribution)

# checks for header files
AC_HEADER_STDC
AC_CHECK_HEADERS(stdlib.h strings.h sys/time.h unistd.h wchar.h stdbool.h)

# checks for typedefs, structures, and compiler characteristics
AC_C_CONST
AC_C_INLINE
AC_HEADER_TIME

# checks for library functions; more autoscan stuff
AC_FUNC_MEMCMP
AC_CHECK_FUNCS(mkdir select socket __sysv_signal)

# Find out the true definitions of some integer types
CIL_CHECK_INTEGER_TYPE(size_t, TYPE_SIZE_T)
CIL_CHECK_INTEGER_TYPE(wchar_t, TYPE_WCHAR_T)


# ----------- platform-specific code -------------
# $target is typically processor-vendor-os
case "$target" in

  *86*linux*)
    AC_MSG_RESULT(configuring for linux/x86)

    ARCHOS=x86_LINUX
    ;;

  *86*openbsd*)
    AC_MSG_RESULT(configuring for OpenBSD/x86)

    ARCHOS=x86_OPENBSD
    ;;

  *i386*freebsd*|*amd64*freebsd*)

    if test x"${ARCH}" = x""; then
       ARCH=`uname -p`
    fi

    AC_MSG_RESULT(configuring for freebsd/${ARCH})

    ARCHOS=${ARCH}_FREEBSD
    ;;

  *powerpc64*linux*)
    AC_MSG_RESULT(configuring for linux/powerpc64)

    ARCHOS=ppc64_LINUX
    ;;

  *powerpc-*linux*)
    AC_MSG_RESULT(configuring for linux/powerpc)

    ARCHOS=ppc_LINUX
    ;;

  # Mac OS X
  *86*darwin*)
    AC_MSG_RESULT(configuring for darwin/x86)

    ARCHOS=x86_DARWIN
    ;;

  *powerpc*darwin*)
    AC_MSG_RESULT(configuring for powerpc/darwin, which we treat like linux/x86)

    ARCHOS=ppc_DARWIN
    ;;

  # cygwin
  *86*cygwin*)
    AC_MSG_RESULT(configuring for Cygwin on win32/x86)

    ARCHOS=x86_WIN32

    # override CILHOME; even on cygwin we want forward slashes
    # sm: I folded this into what I hope will be the only
    # case-analysis of machine type
    #CILHOME=`cygpath -wa "$CILHOME"  | sed -e "s/\\\\\/\\//g"`
    # Try to use the Unix paths even on cygwin. The newest versions of make
    #  do not like colons in file names
    CILHOME=`cygpath -u "$CILHOME"`
    CC=`which $CC`
    CC=`cygpath -wa "$CC"  | sed -e "s/\\\\\/\\//g"`
    # Ocaml >= 3.11 uses flexlink for linking
    if test $major -ge 3 -a $minor -ge 11; then
      FLEXLINK=yes
    fi
    ;;

  # MinGW
  *86*mingw32*)
    AC_MSG_RESULT(configuring for MinGW on win32/x86)

    ARCHOS=x86_WIN32
    MINGW=yes
    # Ocaml >= 3.11 uses flexlink for linking
    if test $major -ge 3 -a $minor -ge 11; then
      FLEXLINK=yes
    fi

    CC=`which $CC`
    ;;

  # Solaris
  *sparc*solaris*)
    AC_MSG_RESULT(configuring for SPARC/Solaris)

    ARCHOS=sparc_SOLARIS
    ;;

  *mipsel*linux*)
    AC_MSG_RESULT(configuring for MIPSel/Linux)

    ARCHOS=mipsel_LINUX
    if test $cross_compiling = "yes"; then
      AC_PATH_PROG([EMUL], [qemu-mipsel])
    fi
    ;;

  *mips*linux*)
    AC_MSG_RESULT(configuring for MIPS/Linux)

    ARCHOS=mips_LINUX
    if test $cross_compiling = "yes"; then
      AC_PATH_PROG([EMUL], [qemu-mips])
    fi
    ;;

  *)
    AC_MSG_ERROR([
      Unsupported platform $target -- sorry.
      ./configure supports these platforms:
         on x86:     Linux, Win32(with Cygwin), FreeBSD, OpenBSD, and Mac OS X
         on amd64:   FreeBSD
         on PowerPC: Mac OS X 
         on SPARC:   Solaris
         on MIPS:    Linux
         on MIPSel:  Linux
    ])
    ;;
esac

# Make the object directory if not already present
AC_MSG_CHECKING([obj/$ARCHOS and related files])
if test -d obj/$ARCHOS; then
    mkdir -p obj/$ARCHOS
fi

rm -f obj/$ARCHOS/machdep.ml
rm -f obj/.depend/machdep.d
rm -f obj/$ARCHOS/feature_config.ml
rm -f obj/.depend/feature_config.d
AC_MSG_RESULT([done])



#
# CIL features
#
#
   
# Set the defaults


# Give a space-separated list of features with the defaults
features="blockinggraph=no zrapp=no llvm=no"

AC_ARG_WITH(blockinggraph,
    AC_HELP_STRING([--with-blockinggraph],
                   [enable the blocking graph feature]))
AC_ARG_WITH(zrapp,
    AC_HELP_STRING([--with-zrapp],
                   [enable the zrapp pretty-printer]))

AC_ARG_WITH(llvm,
    AC_HELP_STRING([--with-llvm],
                   [enable the LLVM code generator]))

# Now add any features specified in the command-line

features="$features $EXTRAFEATURES"

rm -f Makefile.features
echo "# -*- Mode: makefile -*-">Makefile.features
echo "# This file was generated automatically by ./configure.in">>Makefile.features
echo "# We will not need this once configure supports multiline variables">>Makefile.features

for f_val in $features
do
   # If there is no =, then we default to yes
   if ! (echo $f_val | grep "=" >/dev/null) ;then f_val="$f_val=yes"; fi
   # echo "Testing feature $f_val"
   f=`echo $f_val | sed -e s%=.*$%%`
   AC_MSG_CHECKING(whether to use CIL feature $f)
   # default value from "features"
   defval=`echo $f_val | sed -e s%^.*=%%`
   # current value
   getcurval="echo \${with_$f:=$defval}"
   curval=`eval $getcurval`
   AC_MSG_RESULT($curval)
   if test $curval = yes ;then
      CIL_FEATURES="$CIL_FEATURES $f"
      f_up=`echo $f | tr a-z A-Z`
      echo "export USE_$f_up=yes">>Makefile.features
   fi
done
echo "TYPE_SIZE_T=\"$TYPE_SIZE_T\"" >>Makefile.features
echo "TYPE_WCHAR_T=\"$TYPE_WCHAR_T\"" >>Makefile.features
chmod -w Makefile.features


# ----------------- finish up -------------------
# names of the variables that get substituted in files; for example,
# write @ARCHOS@ somewhere in a written file to get it substituted
AC_SUBST(ARCHOS)
AC_SUBST(MINGW)
AC_SUBST(FLEXLINK)
AC_SUBST(EMUL)
AC_SUBST(CILHOME)
AC_SUBST(HAS_MSVC)
AC_SUBST(DEFAULT_COMPILER)
AC_SUBST(DEFAULT_CIL_MODE)
AC_SUBST(CIL_VERSION_MAJOR)
AC_SUBST(CIL_VERSION_MINOR)
AC_SUBST(CIL_VERSION_REV)
AC_SUBST(CIL_VERSION)
AC_SUBST(CYCLES_PER_USEC)
AC_SUBST(HAVE_BUILTIN_VA_LIST)
AC_SUBST(THREAD_IS_KEYWORD)
AC_SUBST(UNDERSCORE_NAME)
AC_SUBST(EXTRAFEATURES)
AC_SUBST(EXTRASRCDIRS)

# finish the configure script and generate various files; ./configure
# will apply variable substitutions to <filename>.in to generate <filename>;

CIL_CONFIG_FILES(Makefile)
CIL_CONFIG_FILES(META)
CIL_CONFIG_FILES(config.mk)
CIL_CONFIG_FILES(test/Makefile)
CIL_CONFIG_EXE_FILES(bin/cilly.bat)
CIL_CONFIG_EXE_FILES(bin/patcher.bat)
CIL_CONFIG_FILES(bin/CilConfig.pm)
CIL_CONFIG_FILES(doc/index.html)
CIL_CONFIG_FILES(doc/header.html)
CIL_CONFIG_FILES(test/llvm/Makefile)
CIL_CONFIG_FILES(src/machdep-ml.c)

AC_OUTPUT()

# show the user what the variables have been set to
cat <<EOF

CIL configuration:
  architecture/OS:            ARCHOS             $ARCHOS
  source tree root:           CILHOME            $CILHOME
  (optional) cl.exe found:    HAS_MSVC           $HAS_MSVC
  gcc to use                  CC                 $CC
  default compiler            DEFAULT_COMPILER   $DEFAULT_COMPILER
  CIL version                 CIL_VERSION        $CIL_VERSION
  CIL features                CIL_FEATURES       $CIL_FEATURES
  Extra source directories    EXTRASRCDIRS       $EXTRASRCDIRS
  Cycles per microsecond      CYCLES_PER_USEC    $CYCLES_PER_USEC
EOF
